<html>
<head><meta charset="utf-8"><title>recursive macros and shadowing · t-lang/pub-macro-rules · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/index.html">t-lang/pub-macro-rules</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html">recursive macros and shadowing</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226140787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226140787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226140787">(Feb 12 2021 at 14:10)</a>:</h4>
<p>This seems to be the major unknown, so I'm opening this topic to discuss. I'd like to at least have an alternative pattern to recommend for people. I'm not sure we need to supply an automatic migration.</p>



<a name="226140811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226140811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226140811">(Feb 12 2021 at 14:10)</a>:</h4>
<p>One option might be having some way to shadow items or have like an "anonymous scope" you can create.</p>



<a name="226790041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226790041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226790041">(Feb 18 2021 at 10:22)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> just to be clear are you talking about recursive macros (i.e., macros that call themselves) or "private" macros (macros that are defined inside of another macro?</p>



<a name="226790362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226790362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226790362">(Feb 18 2021 at 10:25)</a>:</h4>
<p>I <em>think</em> these are two separate questions. I've opened a topic on on "private" macros, and I'd like to use this to discuss recursive macros in the sense of macros that call themselves.</p>



<a name="226790593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226790593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226790593">(Feb 18 2021 at 10:27)</a>:</h4>
<p>I see</p>



<a name="226790629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226790629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226790629">(Feb 18 2021 at 10:27)</a>:</h4>
<p>I meant the pattern of a macro defining multiple versions of a helper, which I guess is the other topic</p>



<a name="226790638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226790638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226790638">(Feb 18 2021 at 10:27)</a>:</h4>
<p>I'm not aware of any problems with "recursive" macros</p>



<a name="226790660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226790660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226790660">(Feb 18 2021 at 10:28)</a>:</h4>
<p>Are there problems?</p>



<a name="226790707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226790707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226790707">(Feb 18 2021 at 10:28)</a>:</h4>
<p>I've been a bit sloppy in my terminology...</p>



<a name="226791353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226791353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226791353">(Feb 18 2021 at 10:33)</a>:</h4>
<p>I don't believe there's a problem with the transition but writing recursive macros might be harder - Macros usually assume that when they are called they are available in scope with an unqualified name or available at $crate::macro. That might no longer be true as macros can be called through paths (i.e., <code>foo::bar::macro!</code>). If <code>foo::bar::macro!</code> recursively calls itself by calling just <code>macro!</code> without the modules, it won't be in scope.</p>



<a name="226811938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/226811938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#226811938">(Feb 18 2021 at 13:50)</a>:</h4>
<p>This would kind of require that suggestion of <code>$self</code> paths in macros, or the <code>match_module_path!</code> workaround I posted on GH</p>



<a name="227465181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227465181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227465181">(Feb 23 2021 at 17:00)</a>:</h4>
<p>So I just confirmed that this is the case in the current implementation.</p>



<a name="227465273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227465273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227465273">(Feb 23 2021 at 17:01)</a>:</h4>
<p>I think adding a $self path makes the most sense. Not having support for this severely limits the usability of macros.</p>



<a name="227470065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227470065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227470065">(Feb 23 2021 at 17:31)</a>:</h4>
<p>I'm not familiar with expansion code, but a quick look makes me think that implementing <code>$self</code> won't be trivial.</p>



<a name="227473345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227473345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227473345">(Feb 23 2021 at 17:50)</a>:</h4>
<p>Hmm, this feels like a problem :)</p>



<a name="227473450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227473450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227473450">(Feb 23 2021 at 17:51)</a>:</h4>
<p>It is, though I don't think of <code>$self</code> as being necessarily a terrible solution</p>



<a name="227473489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227473489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227473489">(Feb 23 2021 at 17:51)</a>:</h4>
<p>It's directly analogous to <code>$crate</code>.</p>



<a name="227474234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227474234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227474234">(Feb 23 2021 at 17:56)</a>:</h4>
<p>it's not .. hmm .. terrible</p>



<a name="227474268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227474268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227474268">(Feb 23 2021 at 17:56)</a>:</h4>
<p>it's just .. more complex</p>



<a name="227474439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227474439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227474439">(Feb 23 2021 at 17:57)</a>:</h4>
<p>Certainly. But path based scoping is more complicated than crate based global namespaces.</p>



<a name="227474686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227474686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227474686">(Feb 23 2021 at 17:59)</a>:</h4>
<p><code>$self</code> has its own problems - it would magically have to allow access to the module where the macro is defined but that macro may not be directly available to the caller.</p>



<a name="227494317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227494317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227494317">(Feb 23 2021 at 20:03)</a>:</h4>
<p>as we said in the meeting, definitely starts to feel like hygiene</p>



<a name="227561837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227561837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227561837">(Feb 24 2021 at 08:04)</a>:</h4>
<p>I thought the goal was to simplify things, not introduce new features.<br>
If some code is difficult to migrate and you think it can be addressed by a new feature, then it is always simpler to provide an option to use <code>macro_rules</code> scoped in the old way instead, because they already implemented and their support won't go away from the compiler any time soon.</p>



<a name="227569322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227569322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227569322">(Feb 24 2021 at 09:17)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> This seemed to be the feeling in the lang team meeting yesterday (i.e., your old plan of just providing an opt-in way to path based scoping). I do think it's a shame there isn't a way to easily do recursive macros in the new system as this is an extremely common pattern.</p>



<a name="227584734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227584734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227584734">(Feb 24 2021 at 11:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123856">Vadim Petrochenkov</span> <a href="#narrow/stream/276890-t-lang.2Fpub-macro-rules/topic/recursive.20macros.20and.20shadowing/near/227561837">said</a>:</p>
<blockquote>
<p>I thought the goal was to simplify things, not introduce new features.</p>
</blockquote>
<p>I agree with this, but I mildly disagree that having some way to "opt back in" to the old system is simpler</p>



<a name="227584746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227584746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227584746">(Feb 24 2021 at 11:31)</a>:</h4>
<p>In particular, an attribute feels very wrong to me</p>



<a name="227584811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227584811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227584811">(Feb 24 2021 at 11:32)</a>:</h4>
<p>It's like now we are saying: we invented a new system, but it has this weird escape hatch that you have to use sometimes that puts you in a whole new world</p>



<a name="227584924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227584924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227584924">(Feb 24 2021 at 11:33)</a>:</h4>
<p>there is a difference to me between an attribute and having pub "opt-in" to a more normal system-- I think I prefer the latter. It's still "macros are different" but it's not "they're the same; but not always"</p>



<a name="227584942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227584942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227584942">(Feb 24 2021 at 11:33)</a>:</h4>
<p>I realize this is squishy though :)</p>



<a name="227589292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227589292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227589292">(Feb 24 2021 at 12:19)</a>:</h4>
<p>I think perhaps we're using "simple" for two different things. There's simple to implement and there's simple to use/explain in the language. Obviously not introducing new concepts like <code>$self</code> is simpler to implement (it's strictly less work), but this doesn't mean it's simpler to use. "Use path based scoping for macros unless your macro is recursive in which case use <code>#[macro_export]</code> is not as simple as "always use path based scoping unless you have some very complex use case"</p>



<a name="227589309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227589309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227589309">(Feb 24 2021 at 12:19)</a>:</h4>
<p>I still think we need to discuss recursive macros and the idea of <code>$self</code>. Ideally almost everything should be expressible in the path based system.</p>



<a name="227639305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227639305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227639305">(Feb 24 2021 at 17:33)</a>:</h4>
<p>Yes, that is a trick. I am definitely think of "simple" for end-users, but even there I feel there are various dimensions.</p>



<a name="227672813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227672813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227672813">(Feb 24 2021 at 21:06)</a>:</h4>
<p>Seems like I confused this case with the "private macro" case from the other thread.</p>



<a name="227673365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227673365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227673365">(Feb 24 2021 at 21:11)</a>:</h4>
<p><code>macro</code> macros can just use def-site hygiene to refer to themselves - <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=752b6922a8326de84ee9d0697bcaf56f">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=752b6922a8326de84ee9d0697bcaf56f</a><br>
So this is more of a "migration problem" rather than a "macro 2.0 problem".</p>



<a name="227674116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227674116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227674116">(Feb 24 2021 at 21:16)</a>:</h4>
<p>For <code>pub macro_rules!</code> <code>$self</code> can always be replaced with <code>$crate::full::path</code>  when necessary - <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ca18d12267289e0d58865ee0f6f82f58">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ca18d12267289e0d58865ee0f6f82f58</a></p>



<a name="227684902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227684902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227684902">(Feb 24 2021 at 22:22)</a>:</h4>
<p>I think the case <span class="user-mention" data-user-id="224872">@rylev</span> had in mind was of somebody defining a recursive macro that gets re-exported out of the private module it was defined in: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=82276ba137cf3fcd7f532bab756a8307">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=82276ba137cf3fcd7f532bab756a8307</a></p>
<p>But the fact that <code>macro</code> macros def_site hygiene already handles this well does suggest that implementing <code>$self</code> may be easier than expected</p>



<a name="227688832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227688832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227688832">(Feb 24 2021 at 22:52)</a>:</h4>
<p>Indeed the case I meant was when the <code>$crate::full::path</code> is not visible to the caller as could be the case when reexporting. </p>
<p>I looked into implementing $self but I would need <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span>’s help to understand how to reuse the logic from <code>macro</code> def site hygiene</p>



<a name="227689350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227689350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227689350">(Feb 24 2021 at 22:57)</a>:</h4>
<p>Here was my attempt at doing it with library code. Oh well <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span> <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=68238880b110b50a5c757d6de96d900d">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=68238880b110b50a5c757d6de96d900d</a></p>



<a name="227690261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227690261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227690261">(Feb 24 2021 at 23:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224872">rylev</span> <a href="#narrow/stream/276890-t-lang.2Fpub-macro-rules/topic/recursive.20macros.20and.20shadowing/near/227688832">said</a>:</p>
<blockquote>
<p>Indeed the case I meant was when the <code>$crate::full::path</code> is not visible to the caller as could be the case when reexporting. </p>
</blockquote>
<p>If the macro is public, then there's at lest one fully visible path to it (possibly involving reexports), which can be used as a <code>$crate::full::path</code>.</p>



<a name="227690661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227690661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227690661">(Feb 24 2021 at 23:07)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232018">Daniel Henry-Mantilla</span> <a href="#narrow/stream/276890-t-lang.2Fpub-macro-rules/topic/recursive.20macros.20and.20shadowing/near/227684902">said</a>:</p>
<blockquote>
<p>But the fact that <code>macro</code> macros def_site hygiene already handles this well does suggest that implementing <code>$self</code> may be easier than expected</p>
</blockquote>
<p>Def-site hygiene is unstable (for multiple reasons), I doubt <code>$self</code> will be useful for migration if it's going to be unstable.<br>
<em>If</em> <code>$self</code> is going to be implemented, which I would prefer to not happen, then it should use stable "mixed-site" hygiene like <code>$crate</code>.</p>



<a name="227741576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227741576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227741576">(Feb 25 2021 at 09:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123856">Vadim Petrochenkov</span> <a href="#narrow/stream/276890-t-lang.2Fpub-macro-rules/topic/recursive.20macros.20and.20shadowing/near/227690261">said</a>:</p>
<blockquote>
<p>If the macro is public, then there's at lest one fully visible path to it (possibly involving reexports), which can be used as a <code>$crate::full::path</code>.</p>
</blockquote>
<p>one problem is that the <em>macro</em> may not know the path</p>



<a name="227741675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227741675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227741675">(Feb 25 2021 at 09:46)</a>:</h4>
<p>but perhaps you meant something like <code>$self</code> where the macro uses a shorthand that is expanded at the point of use, <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> ?</p>



<a name="227742785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227742785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227742785">(Feb 25 2021 at 09:56)</a>:</h4>
<blockquote>
<p>one problem is that the macro may not know the path</p>
</blockquote>
<p>The person who writes the macro certainly knows the path.</p>



<a name="227742938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227742938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227742938">(Feb 25 2021 at 09:57)</a>:</h4>
<p>I don't think that's true in a cross-crate re-export scenario</p>



<a name="227743025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227743025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227743025">(Feb 25 2021 at 09:58)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// What happens here?</span>

<span class="c1">// crate A</span>
<span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">pub</span><span class="w"> </span><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$crate</span>::<span class="n">foo</span>::<span class="n">m</span><span class="o">!</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// crate B</span>
<span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">crate_a</span>::<span class="n">foo</span>::<span class="n">m</span><span class="p">;</span><span class="w"></span>

<span class="c1">// crate C</span>
<span class="n">crate_b</span>::<span class="n">m</span><span class="p">();</span><span class="w"></span>
</code></pre></div>



<a name="227743059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227743059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227743059">(Feb 25 2021 at 09:58)</a>:</h4>
<p>(from <a href="https://hackmd.io/WkFGXS8GTxCYcv9MylYBRA?view#%E2%80%9CChange-visibility-scoping-rules-for-macro_rules-macros%E2%80%9D-rfcs3067">lang team minutes</a>, which I just realized I never published)</p>



<a name="227743093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227743093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227743093">(Feb 25 2021 at 09:58)</a>:</h4>
<p>does that work?</p>



<a name="227743107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227743107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227743107">(Feb 25 2021 at 09:58)</a>:</h4>
<p>it depends on what <code>$crate</code> does, we weren't sure</p>



<a name="227743167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227743167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227743167">(Feb 25 2021 at 09:59)</a>:</h4>
<p>and in particular whether <code>$crate</code> acts "hygienically" -- i.e., maybe crate C doesn't <em>directly</em> depend on crate A</p>



<a name="227743965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227743965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227743965">(Feb 25 2021 at 10:05)</a>:</h4>
<p>Just tested <code>$crate</code> and it can "peak through" visibility.</p>



<a name="227744043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744043">(Feb 25 2021 at 10:06)</a>:</h4>
<p>you tested it with <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span>'s PR, <span class="user-mention" data-user-id="224872">@rylev</span> ?</p>



<a name="227744062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744062">(Feb 25 2021 at 10:06)</a>:</h4>
<p>what exactly did you test, I guess is my question :)</p>



<a name="227744090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744090">(Feb 25 2021 at 10:06)</a>:</h4>
<p>No I tested using <code>$crate</code> on the stable compiler</p>



<a name="227744104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744104">(Feb 25 2021 at 10:07)</a>:</h4>
<p>how does visibility play in then?</p>



<a name="227744163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744163">(Feb 25 2021 at 10:07)</a>:</h4>
<p>$crate can see through visbility. I.e., Your example above works even though crate C does not have access to cratea::foo</p>



<a name="227744317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744317">(Feb 25 2021 at 10:08)</a>:</h4>
<p>Which means <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> is correct that there is always a public path that the macro can refer to (using $crate) that will work no matter who is calling the macro</p>



<a name="227744318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744318">(Feb 25 2021 at 10:08)</a>:</h4>
<p>my example above, but translated into <code>#[macro_export]</code> ?</p>



<a name="227744335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744335">(Feb 25 2021 at 10:08)</a>:</h4>
<p>Ah yes sorry</p>



<a name="227744402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744402">(Feb 25 2021 at 10:09)</a>:</h4>
<p>ok, so <code>$crate</code> expands to "the crate where the macro was defined, even if the user couldn't name it", in other words</p>



<a name="227744417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744417">(Feb 25 2021 at 10:09)</a>:</h4>
<p>We can't yet test it against <code>pub</code> since this doesn't yet work across crates. <span class="user-mention" data-user-id="116266">@Santiago Pastorino</span> is looking into fixing this (there's a problem with metadata currently)</p>



<a name="227744427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744427">(Feb 25 2021 at 10:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/276890-t-lang.2Fpub-macro-rules/topic/recursive.20macros.20and.20shadowing/near/227744402">said</a>:</p>
<blockquote>
<p>ok, so <code>$crate</code> expands to "the crate where the macro was defined, even if the user couldn't name it", in other words</p>
</blockquote>
<p>Yes it seems so</p>



<a name="227744472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744472">(Feb 25 2021 at 10:10)</a>:</h4>
<p>yes, this is why I was trying to figure out what you meant</p>



<a name="227744495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744495">(Feb 25 2021 at 10:10)</a>:</h4>
<p>but if you kept the <em>definition of the macro</em> the same--</p>



<a name="227744515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744515">(Feb 25 2021 at 10:10)</a>:</h4>
<p>i.e., it still used <code>$crate::foo::m!</code>...</p>



<a name="227744531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744531">(Feb 25 2021 at 10:10)</a>:</h4>
<p>...I don't think I realized that was stable</p>



<a name="227744554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744554">(Feb 25 2021 at 10:10)</a>:</h4>
<p>can you just paste exactly what you tested? :)</p>



<a name="227744950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227744950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227744950">(Feb 25 2021 at 10:14)</a>:</h4>
<p>crate <code>foo</code> (depends on crate <code>bar</code>):</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">bar</span>::<span class="n">my_macro</span><span class="o">!</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">CONST</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>crate <code>bar</code> (depends on crate <code>baz</code>):</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">baz</span>::<span class="n">my_macro</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>crate <code>baz</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[macro_export]</span><span class="w"></span>
<span class="fm">macro_rules!</span><span class="w"> </span><span class="n">my_macro</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">       </span><span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="cp">$crate</span>::<span class="n">foo</span>::<span class="n">bar</span>::<span class="n">CONST</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">CONST</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227745018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227745018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227745018">(Feb 25 2021 at 10:14)</a>:</h4>
<p>The above compiles without error ^^</p>



<a name="227745052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227745052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227745052">(Feb 25 2021 at 10:15)</a>:</h4>
<p>even though <code>foo</code> cannot directly name <code>baz::foo::bar::CONST</code></p>



<a name="227745382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227745382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227745382">(Feb 25 2021 at 10:18)</a>:</h4>
<p>I'm slightly surprised by this, but it does seem consistent, and means (most likely) that we can create recursive macros in the new system.</p>



<a name="227746842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227746842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227746842">(Feb 25 2021 at 10:31)</a>:</h4>
<p>Of course, this doesn't help with local only recursive macros that are not available from the crate root.</p>



<a name="227747775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227747775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227747775">(Feb 25 2021 at 10:40)</a>:</h4>
<p>cool</p>



<a name="227757970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227757970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227757970">(Feb 25 2021 at 12:21)</a>:</h4>
<p>So we have a way of allowing cross crate recursive macros but still need to think about the fact that crate private macros cannot be used in locations where the macro cannot be directly accessed from the crate root.</p>



<a name="227783604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227783604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227783604">(Feb 25 2021 at 15:20)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> I've not been able to think of a way that allows crate local macros to always work (outside of a theoretical <code>$self</code>). Do you have any thoughts here?</p>



<a name="227825111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/276890-t-lang/pub-macro-rules/topic/recursive%20macros%20and%20shadowing/near/227825111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/276890-t-lang/pub-macro-rules/topic/recursive.20macros.20and.20shadowing.html#227825111">(Feb 25 2021 at 19:39)</a>:</h4>
<p>I think this shouldn't be a problem in practice because in a local crate everything is under control of the macro's author and modules/imports can always be restructured to make the macro's full path (or full path of one of its imports/reexports) accessible from all points in which the macro is used.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>